package com.fyi.controller;

import com.fyi.entity.Admin;
import com.fyi.entity.Article;
import com.fyi.service.AdminService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    private static final Log logger = LogFactory.getLog(AdminController.class);

    @Autowired
    private AdminService adminService;


    /**
     * 登录
     */
    @RequestMapping("superAdminLoginView")
    public String superAdminLoginView(){
        return "superAdminLogin";
    }
    @RequestMapping("/superAdminLogin")
    public String superAdmin(String adminName, String password , HttpSession session, Model model){

        if (adminService.login(adminName,password)!=null) {
            session.setAttribute("adminName",adminName);
            session.setAttribute("password",password);
            logger.info("登录成功");
            return "backView/backIndex";
        }else {
            logger.info("登录失败！");
            model.addAttribute("messageError","用户名或密码错误");
            return "superAdminLogin";
        }
    }
    /**
     * 注册
     * 添加管理员
    /* */
    @RequestMapping("addSuperAdmin")
    @ResponseBody
    public Map addSuperAdmin(@ModelAttribute Admin admin){

        Date d=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:dd:ss");
        String time=sdf.format(d);
        System.out.println("--"+time);
        admin.setCreateAdminTime(time);

        Integer addAdmin = adminService.addAdmin(admin);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "添加成功");
        result.put("data", addAdmin);
        return result;
    }
    /**
     * 页面跳转
     */
    @RequestMapping("showArticleView")
    public String showArticleView(){
        return "showArticle";
    }
    @RequestMapping("technicalExchangeView")
    public String technicalExchangeView(){
        return "technicalExchange";
    }
    @RequestMapping("aboutFyiView")
    public String aboutFyiView(){
        return "aboutFyi";
    }
    @RequestMapping("articleIndexView")
    public String articleIndexView(){
        return "articleIndex";
    }

    @RequestMapping("pages404View")
    public String pages404View(){
        return "backView/pages404";
    }

    /**
     * 根据Id查询
     */
    @RequestMapping("/selectAdminById/{adminId}")
    @ResponseBody
    public Map selectArticeById(@PathVariable Integer adminId){
        Admin selectById = adminService.selectById(adminId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectById);
        return result;
    }

    /**
     * 查看所有管理员
     */
    @RequestMapping("superAdminView")
    public String superAdminView(){
        return "backView/superAdmin";
    }
    @RequestMapping("/selectAllAdmin")
    @ResponseBody
    public Map selectAllAdmin(){
        List<Admin> selectAllAdmin = adminService.selectAllAdmin();
        System.out.println(selectAllAdmin);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data", selectAllAdmin);
        return result;
    }
    /**
     * 删除
     */
    @RequestMapping("/deleteAdmin/{adminId}")
    @ResponseBody
    public Map deleteAdmin(@PathVariable Integer adminId){
        Integer deleteAdmin = adminService.deleteAdmin(adminId);
        Map result = new HashMap();
        result.put("code", 200);
        result.put("msg", "删除成功");
        result.put("data", deleteAdmin);
        return result;
    }
}
